package com.example.shirotest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shirotest.base.ResultVo;
import com.example.shirotest.entry.HtmlRegex;
import com.example.shirotest.entry.Result;
import com.example.shirotest.entry.Sites;
import com.example.shirotest.entry.TableList;
import com.example.shirotest.mapper.SitesMapper;
import com.example.shirotest.service.SitesService;
import com.example.shirotest.utils.HtmlParse;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/sites")
@Controller
public class SitesController extends BashController{

    @Autowired
    private SitesMapper sitesMapper;
    @Autowired
    private SitesService sitesService;

    @RequestMapping("/index")
    public String index(Model model){
        setUserInfo(model);
        model.addAttribute("tag_mb","sites-index");
        return "sites/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableList resultlist(Integer pagenum, Integer pagesize, String sort, String order){
        PageHelper.startPage(pagenum,pagesize,sort+" "+order);
        List<Sites> pages = sitesMapper.selectAll();
        PageInfo pageInfo = new PageInfo<Sites>(pages);

        //封装输出数据
        TableList<Result> TableList = new TableList<>();
        TableList.setList(pageInfo.getList());
        TableList.setTotal((int)pageInfo.getTotal());

        return TableList;
    }


    //修改账号状态
    @RequestMapping("/state")
    @ResponseBody
    public ResultVo locked(Integer id, Integer state){
        if(sitesService.updateSitesState(id, state)){
            return new  ResultVo(1,"操作成功","");
        }

        return new  ResultVo(0,"操作失败","");
    }


    //添加数据源
    @RequestMapping("/edit")
    public String edit(Model model, @RequestParam(defaultValue = "0",value = "id") int id){
        setUserInfo(model);

        String title = "";
        String url   = "";
        String regex = "";
        if(id>0){
            Sites sites = sitesService.getById(id);
            if(sites!=null) {
                title = sites.getTitle();
                url   = sites.getUrl();
                regex = sites.getRegex();
            }
        }

        model.addAttribute("id",id);
        model.addAttribute("title",title);
        model.addAttribute("url",url);
        if(!regex.equals("")){
            Gson gson = new Gson();
            HtmlRegex htmlRegex = gson.fromJson(regex, HtmlRegex.class);
            model.addAttribute("htmlRegex",htmlRegex);
        }else{
            model.addAttribute("htmlRegex",new HtmlRegex());
        }

        model.addAttribute("tag_mb","sites-edit");
        return "sites/edit";
    }

    @PostMapping("/appendsite")
    @ResponseBody
    public ResultVo appendsite(Sites sites){
        //新增
        if(sites.getId()==0){
            if(sitesService.isExist(sites))return new ResultVo(0,"该数据源已存在",null);

            if(sitesService.addSites(sites)){
                return new ResultVo(1,"添加成功",null);
            }

            return new ResultVo(0,"添加失败",null);
        }

        //修改
        if(sitesService.updateById(sites)){
            return new ResultVo(1,"修改成功",null);
        }

        return new ResultVo(0,"修改失败",null);
    }

    //抓取并保存数据
    @RequestMapping("/gethref")
    @ResponseBody
    public ResultVo getHref(String url,String htmlRegex){
        Gson gson = new Gson();
        HtmlRegex htmlRegex1 = gson.fromJson(htmlRegex, HtmlRegex.class);

        ArrayList<Result> results = null;

            try {
                results = HtmlParse.getNetContent(url,htmlRegex1);
            } catch (IOException e) {
                return new ResultVo(0,e.getMessage(),null);
            }

            if(results==null){
                return new ResultVo(0,"没有数据",null);
            }



            return new  ResultVo(1,"成功",results);
    }

}
